/*
 *  txdnet.cn tonydon
 * 
 */
package cn.txdnet.txcms.admin.action;

import cn.txdnet.txcms.domain.HotShop;
import cn.txdnet.txcms.query.HotShopQuery;
import cn.txdnet.txcms.service.HotShopService;
import cn.txdnet.txweb.annotation.methods.PostMethod;
import cn.txdnet.txweb.model.ModelUtil;
import cn.txdnet.txweb.model.ModelView;
import cn.txdnet.txweb.utils.BeanUtil;
import cn.txdnet.txweb.utils.Message;
import cn.txdnet.txweb.utils.WebUtil;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author txdnet
 */
public class HotShopAction {

    /**
     * 保存shop信息
     *
     * @param req
     * @return
     * @throws Exception
     */
    @PostMethod
    public ModelView save(HttpServletRequest req) throws Exception {
        HotShop shop = BeanUtil.parseRequestToBean(HotShop.class, req);
        if (!shop.validate()) {
            return ModelUtil.getTextModel(Message.MSG_FIELD_ERR);
        }
        if (HotShopService.make(shop)) {
            return ModelUtil.getTextModel(Message.MSG_SUCCESS);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 更新SHOP信息
     *
     * @param req
     * @return
     * @throws Exception
     */
    @PostMethod
    public ModelView update(HttpServletRequest req) throws Exception {
        HotShop shop = BeanUtil.parseRequestToBean(HotShop.class, req);
        if (!shop.validate()) {
            return ModelUtil.getTextModel(Message.MSG_FIELD_ERR);
        }
        if (HotShopService.update(shop)) {
            return ModelUtil.getTextModel(Message.MSG_SUCCESS);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新发布状态
     *
     * @param req
     * @return
     */
    public ModelView update_pubstate(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        byte pubstate = WebUtil.getByte(req, "pubstate", -1);
        if (ids == null || pubstate < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = HotShopService.updatePubstate(ids, pubstate);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新评论状态
     *
     * @param req
     * @return
     */
    public ModelView update_cmtstate(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        byte iscmt = WebUtil.getByte(req, "iscmt", -1);
        if (ids == null || iscmt < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = HotShopService.updateCmtstate(ids, iscmt);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新发布时间
     *
     * @param req
     * @return
     */
    public ModelView update_pubtime(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        long pubtime = WebUtil.getLong(req, "pubtime", -1);
        if (ids == null || pubtime < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = HotShopService.updatePubTime(ids, pubtime);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新时间
     *
     * @param req
     * @return
     */
    public ModelView update_time(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        long updatetime = WebUtil.getLong(req, "updatetime", -1);
        if (ids == null || updatetime < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = HotShopService.updateTime(ids, updatetime);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新类目信息
     *
     * @param req
     * @return
     */
    public ModelView update_catelog(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        int cid = WebUtil.getInt(req, "cid", -1);
        String ctp = WebUtil.getNoNull(req, "ctp", null);
        if (ids == null || cid < 0 || ctp == null) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = HotShopService.updateCatalogBatch(ids, cid, ctp);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量删除信息
     *
     * @param req
     * @return
     */
    public ModelView delete_batch(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        if (ids == null) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int del_num = HotShopService.deleteBatch(ids);
        if (del_num > 0) {
            return ModelUtil.getTextModel(del_num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 更具条件查询记录
     *
     * @param req
     * @return
     * @throws Exception
     */
    public ModelView list_factor(HttpServletRequest req) throws Exception {
        HotShopQuery query = BeanUtil.parseRequestToBean(HotShopQuery.class, req);
        return ModelUtil.getJsonModel(HotShopService.queryByFactor(query));
    }

    /**
     * 初始化信息编辑表单界面
     *
     * @param req
     * @return
     */
    public ModelView query_shop_edit(HttpServletRequest req) {
        long shopid = WebUtil.getLong(req, "shopid", -1);
        if (shopid < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        HotShop shop = HotShopService.queryShopForEdit(shopid);
        if (shop == null) {
            return ModelUtil.getTextModel(Message.MSG_VALUE_NULL);
        }
        return ModelUtil.getJsonModel(shop);
    }
}
